import { _decorator, Collider2D, Component, Contact2DType, Node } from 'cc';
const { ccclass, property } = _decorator;

export enum RewardType {
    TwoShoot,
    Bomb
}

@ccclass('Reward')
export class Reward extends Component {

    @property
    speed:number = 120;
    @property
    rewardType:RewardType = RewardType.TwoShoot;

    collider:Collider2D = null;

    start() {
        this.collider = this.getComponent(Collider2D);
        if(this.collider) {
            this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
    }

    onBeginContact() {
        console.log('onBeginContact reward ...');
        this.scheduleOnce(function() {
            this.node.destroy();
        }, 0.1);
    }

    update(deltaTime: number) {
        const p = this.node.position;
        this.node.setPosition(p.x, p.y - deltaTime*this.speed, p.z);

        if(this.node.position.y < -580) {
            this.node.destroy();
        }
    }

    protected onDestroy(): void {
        if(this.collider) {
            this.collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
    }
    
}

